package com.woniu.defecttracking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.defecttracking.annotation.AddNum;
import com.woniu.defecttracking.commens.ComUtil;
import com.woniu.defecttracking.dto.AddProjectDto;
import com.woniu.defecttracking.dto.BugInfosDto;
import com.woniu.defecttracking.dto.FindProjectDto;
import com.woniu.defecttracking.dto.PageProjectDto;
import com.woniu.defecttracking.entity.AccountDetails;
import com.woniu.defecttracking.entity.Project;
import com.woniu.defecttracking.mapper.ProjectMapper;
import com.woniu.defecttracking.param.AddProjectParam;
import com.woniu.defecttracking.param.FindProjectParam;
import com.woniu.defecttracking.param.UpdateProjectParam;
import com.woniu.defecttracking.service.AccountService;
import com.woniu.defecttracking.service.ProjectAccountService;
import com.woniu.defecttracking.service.ProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.aspectj.lang.annotation.Around;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 何家欣
 * @since 2021-12-31
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectService {
    @Autowired
    private AccountService accountService;
    @Autowired
    private ProjectAccountService projectAccountService;
    @Override
    /**
    * @作者: Administrator
    * @描述：
    * @时间：2022/1/2 15:19
    * @参数：[findProjectParam]
    * @返回：com.woniu.defecttracking.dto.PageProjectDto
    **/
    public PageProjectDto findProject(FindProjectParam findProjectParam) {
        Project project = ComUtil.copyProperties(findProjectParam, Project.class);
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>(project);
        queryWrapper.eq("project_isdel",0);
        //模糊查询
        if (findProjectParam.getKeywords()!=null){
            queryWrapper.and(qw ->{
                qw.like("project_num",findProjectParam.getKeywords()).or()
                    .like("project_desc",findProjectParam.getKeywords()).or()
                .like("project_name",findProjectParam.getKeywords()).or()
                    .like("project_state",findProjectParam.getKeywords());
            });

        }

        //排序
        queryWrapper.orderByDesc("project_id");
        //分页
        Page<Project> objectPage = new Page<>();
        objectPage.setCurrent(findProjectParam.getPageIndex());
        objectPage.setSize(findProjectParam.getPageSize());
        Page<Project> page = page(objectPage, queryWrapper);


        PageProjectDto pageProjectDto = new PageProjectDto();
        pageProjectDto.setTotal(page.getTotal());
        pageProjectDto.setIndexPage(page.getCurrent());
        List<Project> records = page.getRecords();
        List<FindProjectDto> findProjectDtos = ComUtil.copyList(records, FindProjectDto.class);
        pageProjectDto.setFindProjectDtos(findProjectDtos);
        return pageProjectDto;
    }
    @AddNum("projectNum")
    @Override
    public AddProjectDto addProject(AddProjectParam addProjectParam) {
        if (addProjectParam==null){
            return null;
        }
        Project project = ComUtil.copyProperties(addProjectParam, Project.class);

        if (save(project)){
            AddProjectDto addProjectDto = ComUtil.copyProperties(project, AddProjectDto.class);
            return addProjectDto;

        }
        return null;
    }

    @Override
    public boolean updateProject(UpdateProjectParam updateProjectParam) {
        Project project = ComUtil.copyProperties(updateProjectParam, Project.class);
        if (updateById(project)){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteProject(List<Integer> ids) {
        List<Project> projects = listByIds(ids);
        System.out.println(projects.size());
        projects.forEach(project ->{
            project.setProjectIsdel(1);
        });
        saveOrUpdateBatch(projects);

        return true;
    }

    @Override

    public FindProjectDto findAllProject() {
        //返回开启状态的
        QueryWrapper<Project> qw = new QueryWrapper<>();
        qw.eq("project_isdel",0);
        qw.eq("project_state",1);

        AccountDetails principal = (AccountDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer accountSupper = accountService.getById(principal.getAccountId()).getAccountSupper();

        if (accountSupper==0){
            List<Integer> projects = projectAccountService.getProjects(principal.getAccountId());
            if (projects.size()<=0){
                return new FindProjectDto();
            }
            qw.in("project_id",projects);
        }

        List<Project> list = list(qw);
        FindProjectDto findProjectDto = new FindProjectDto();
        List<FindProjectDto> findProjectDtos = ComUtil.copyList(list, FindProjectDto.class);
        findProjectDto.setProjects(findProjectDtos);
        return findProjectDto;
    }

    @Override
    public boolean updateStateUp(Integer id) {
        QueryWrapper<Project> objectQueryWrapper = new QueryWrapper<>();
        Project projectId = getOne(objectQueryWrapper.eq("project_id",id));
        if (projectId.getProjectState()==1){
            return false;
        }
        projectId.setProjectState(1);
        if (updateById(projectId)){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateStateDown(Integer id) {
        QueryWrapper<Project> objectQueryWrapper = new QueryWrapper<>();
        Project projectId = getOne(objectQueryWrapper.eq("project_id",id));
        if (projectId.getProjectState()==0){
            return false;
        }
        projectId.setProjectState(0);
        if (updateById(projectId)){
            return true;
        }
        return false;
    }

    @Override
    public FindProjectDto findOneProject(Integer id) {
        Project byId = getById(id);
        FindProjectDto findProjectDto = ComUtil.copyProperties(byId, FindProjectDto.class);
        return findProjectDto;
    }


}
